 using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Automation;
using System.Timers;
using System.Net.Sockets;
using System.Threading;

namespace InternetMonitoring
{
    class InternetMonitoring
    {

        private static System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
        private static NetworkStream serverStream = default(NetworkStream);
        private static string readData = string.Empty;

        private static System.Timers.Timer timer;
        private static IntPtr phandle = IntPtr.Zero;
        private static IntPtr fphandle = IntPtr.Zero;
        private static string chromeurl = string.Empty;
        private static string firefoxurl = string.Empty;
        private static Process[] p;
        private static Process[] fp;

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool IsWindow(IntPtr hWnd);

        public static string GetURL(IntPtr handle)
        {
            if (handle == IntPtr.Zero)
                return "is not opened!";
            AutomationElement element = AutomationElement.FromHandle(handle); //main control of the Chrome browser
            AutomationElement editControl; //this will contain the edit control of the Chrome browser

            Condition propCondition = new PropertyCondition(
                AutomationElement.ControlTypeProperty, ControlType.Edit, PropertyConditionFlags.None);

            // Find the element. 
            editControl = element.FindFirst(TreeScope.Element | TreeScope.Children | TreeScope.Descendants, propCondition);

            return ((ValuePattern)editControl.GetCurrentPattern(ValuePattern.Pattern)).Current.Value;
        }

        static string getString(string msg)
        {
            Console.WriteLine(msg);
            return Console.ReadLine();
        }

        static void Main(string[] args)
        {

            clientSocket.Connect(getString("Ip address of server: "), 8888);
            serverStream = clientSocket.GetStream();

            sendMessage(serverStream, System.Environment.MachineName + "$");

            timer = new System.Timers.Timer(1000);
            timer.Elapsed += timer_Elapsed;
            timer.Start();


            Console.WriteLine("Press enter to disconnect from the server.");
            Console.ReadLine();
        }

        static void sendMessage(NetworkStream serverStream, string message)
        {
            byte[] outStream = System.Text.Encoding.ASCII.GetBytes(message + "$");
            serverStream.Write(outStream, 0, outStream.Length);
            serverStream.Flush();
        }

        static void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            chromeurl = string.Empty;
            firefoxurl = string.Empty;
            p = Process.GetProcessesByName("Chrome");
            fp = Process.GetProcessesByName("Firefox");
            phandle = new IntPtr();
            fphandle = new IntPtr();
            foreach (Process proc in p)
            {
                phandle = proc.MainWindowHandle;
                if (IsWindow(phandle))
                    break;
            }
            foreach (Process proc in fp)
            {
                fphandle = proc.MainWindowHandle;
                if (IsWindow(fphandle))
                    break;
            }
            chromeurl = GetURL(phandle);
            firefoxurl = GetURL(fphandle);

            sendMessage(serverStream, "\nChrome: " + chromeurl + "\nFirefox: " + firefoxurl + "\n$");

            timer.Interval = 10000; //10 seconds update interval
        }

    }
}
